﻿#region License

/*
 © 2012 Alex Kazaev
 * FolderDrive project [http://folderdrive.codeplex.com/]
 * Licensed under Ms-PL license [http://www.opensource.org/licenses/MS-PL]
 */

#endregion License

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace FolderDrive.Configuration
{
  /// <summary>
  /// Class that manages the configuration, stores and gets parameters from it
  /// </summary>
  public static class FolderDriveConfigurationManager
  {
    #region fields

    static System.Configuration.Configuration UserConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
    static FolderDriveConfigurationSection mappingSection;

    #endregion fields

    /// <summary>
    /// Gets or creates the mapping section.
    /// </summary>
    private static FolderDriveConfigurationSection MappingSection
    {
      get
      {
        if (mappingSection == null)
        {
          GetOrCreateMappingSection();
        }
        return mappingSection;
      }
    }

    #region methods

    /// <summary>
    /// Gets the drive mappings from config.
    /// </summary>
    /// <returns>the dictionary filled with drive letters as keys and paths as values</returns>
    public static Dictionary<string, string> GetDriveMappings()
    {
      return MappingSection.Drives.Cast<FolderDriveConfigurationElement>().ToDictionary(drive => drive.DriveLetter, drive => drive.Path);
    }

    /// <summary>
    /// Returns the permanent flag value
    /// </summary>
    /// <param name="letter">the drive letter</param>
    /// <returns>the flag value</returns>
    public static bool GetPermanentFlag(string letter)
    {
      for (int i = 0; i < MappingSection.Drives.Count; i++)
      {
        if (MappingSection.Drives[i].DriveLetter.Equals(letter, StringComparison.InvariantCultureIgnoreCase))
        {
          return MappingSection.Drives[i].IsPermanent;
        }
      }
      return false;
    }

    /// <summary>
    /// Gets the check updates flag.
    /// </summary>
    /// <returns>the check updates flag value</returns>
    public static bool GetCheckUpdates()
    {
        return MappingSection.CheckUpdates;
    }

    /// <summary>
    /// Gets the minimize on close flag.
    /// </summary>
    /// <returns>the minimize on close flag value</returns>
    public static bool GetMinOnClose()
    {
        return MappingSection.MinOnClose;
    }

    /// <summary>
    /// Gets the unmount on exit flag.
    /// </summary>
    /// <returns>the unmount on exit flag value</returns>
    public static bool GetUnmountOnExit()
    {
      return MappingSection.UnmountOnExit;
    }

    /// <summary>
    /// Gets the start minimized flag.
    /// </summary>
    /// <returns>the start minimized flag value</returns>
    public static bool GetStartMinimized()
    {
      return MappingSection.StartMinimized;
    }

    /// <summary>
    /// Gets the show tooltip flag.
    /// </summary>
    /// <returns>the show tooltip flag value</returns>
    public static bool GetShowTooltip()
    {
      return MappingSection.ShowTooltip;
    }

    /// <summary>
    /// Sets the minimize on close flag value.
    /// </summary>
    /// <param name="value">the flag value</param>
    public static void SetMinOnClose(bool value)
    {
        MappingSection.MinOnClose = value;
        UserConfig.Save();
    }

    /// <summary>
    /// Sets the check updates flag value.
    /// </summary>
    /// <param name="value">the flag value</param>
    public static void SetCheckUpdates(bool value)
    {
        MappingSection.CheckUpdates = value;
        UserConfig.Save();
    }

    /// <summary>
    /// Sets the unmount on exit flag value.
    /// </summary>
    /// <param name="value">the flag value</param>
    public static void SetUnmountOnExit(bool value)
    {
      MappingSection.UnmountOnExit = value;
      UserConfig.Save();
    }

    /// <summary>
    /// Sets the start minimized flag value.
    /// </summary>
    /// <param name="value">the flag value</param>
    public static void SetStartMinimized(bool value)
    {
      MappingSection.StartMinimized = value;
      UserConfig.Save();
    }

    /// <summary>
    /// Sets the show tooltip flag value.
    /// </summary>
    /// <param name="value">the flag value</param>
    public static void SetShowTooltip(bool value)
    {
      MappingSection.ShowTooltip = value;
      UserConfig.Save();
    }

    /// <summary>
    /// Adds the mapping to the drive mappings collection.
    /// </summary>
    /// <param name="letter">The drive letter.</param>
    /// <param name="path">The path.</param>
    /// <param name="permanent">determines if the mapping is permanent</param>
    public static void AddMapping(string letter, string path, bool permanent)
    {
      FolderDriveConfigurationElement configElement = new FolderDriveConfigurationElement
                                                        {
                                                          Path = path,
                                                          DriveLetter = letter,
                                                          IsPermanent = permanent
                                                        };
      MappingSection.Drives.Add(configElement);

      UserConfig.Save();
    }

    /// <summary>
    /// Removes the mapping from the drive mappings collection.
    /// </summary>
    /// <param name="letter">The drive letter.</param>
    public static void RemoveMapping(string letter)
    {
      for (int i = 0; i < MappingSection.Drives.Count; i++)
      {
        if (MappingSection.Drives[i].DriveLetter.Equals(letter, StringComparison.InvariantCultureIgnoreCase))
        {
          MappingSection.Drives.RemoveAt(i);
          break;
        }
      }
      UserConfig.Save();
    }

    /// <summary>
    /// Gets the or creates the config section.
    /// </summary>
    /// <returns>the config section</returns>
    private static void GetOrCreateMappingSection()
    {
      mappingSection = UserConfig.GetSection("mapping") as FolderDriveConfigurationSection;
      if (mappingSection == null)
      {
        mappingSection = new FolderDriveConfigurationSection();
        mappingSection.SectionInformation.AllowExeDefinition =
                 ConfigurationAllowExeDefinition.MachineToLocalUser;
        UserConfig.Sections.Add("mapping", mappingSection);
        UserConfig.Save();
      }
    }

    #endregion methods
  }
}